let STA = require("STA");

cc.Class({
    extends: cc.Component,
    properties: {
        bottomList: cc.Node,
        gameStartBtn: cc.Node,
        soundBtn:cc.Node,
        tapSound:cc.AudioClip,
        gameLoadSound:cc.AudioClip,

        tipFrame:cc.Node,

        rankBlank:cc.Node,
        myRank:cc.Node,
        aRank:cc.Prefab,
        rankContent:cc.Node,
    },

    showLog(data){
        this.testText.getComponent(cc.Label).string += "\n"+data;
    },

    onLoad () {
        this.tipFrame.active = false;
        this.rankBlank.active = false;
        this.soundBtn.active = STA.canPlayAudio;

        STA.showStaticAD();
        STA.getUserData();


        if(STA.isFirstIn){
            // STA.playAudioBK("gameload");
            STA.playAudio(this.gameLoadSound);
            STA.isFirstIn = false;
        }
        if(STA.isQQ){
            BK.onEnterBackground(()=>{
                cc.audioEngine.stopAll();
                // for(let sound of STA.sounds){
                //     delete sound;
                // }
            });
        }

        this.gameStartBtn.on(cc.Node.EventType.TOUCH_START, (event)=>{
            this.gameStartBtn.runAction(cc.sequence(
                cc.scaleTo(0.2,1.2),
                cc.scaleTo(0.2,1),
                cc.callFunc(()=>{
                    cc.director.preloadScene("bigChapters", function () {
                        cc.director.loadScene("bigChapters");
                    });
                })
            ));
            STA.playAudio(this.tapSound);
        });

        let btns = this.bottomList.children;
        for(let btn of btns){
            btn.on(cc.Node.EventType.TOUCH_START, (event)=>{
                btn.runAction(cc.sequence(
                    cc.scaleTo(0.15,1.1),
                    cc.scaleTo(0.15,1)
                ));
                STA.playAudio(this.tapSound);
            });
        }
    },

    start () {
        
    },

    toggleSound(){
        if(STA.canPlayAudio){
            STA.canPlayAudio = false;
            this.soundBtn.active = false;
        }else{
            STA.canPlayAudio = true;
            this.soundBtn.active = true;
        }
    },

    showRank(event){
        if(!STA.isQQ) return;

        function getTheRankNumImg(obj,num){
            if(num <= 3){
                cc.loader.loadRes("rankNumImg/rank"+num, cc.SpriteFrame,(err,spriteFrame)=>{
                    if(err) STA.log(err);
                    obj.addComponent(cc.Sprite).spriteFrame = spriteFrame;
                });
            }else{
                obj.color = new cc.Color(150,150,150);
                obj.addComponent(cc.Label).string = num;
            }
        }

        this.rankContent.removeAllChildren();
        this.rankBlank.active = true;
        this.aRankHeight = 130;
        this.rankList = null;

        // 当前不支持一次同时拉取多个排行榜，需要拉取多次，而且必须等上一个拉取回来后才能拉取另外一个排行榜
        let attr = "score"; 
        let order = 1;     //排序的方法：[ 1: 从大到小(单局)，2: 从小到大(单局)，3: 由大到小(累积)]
        let rankType = 0; //要查询的排行榜类型，0: 好友排行榜
        // 必须配置好周期规则后，才能使用数据上报和排行榜功能
        BK.QQ.getRankListWithoutRoom(attr, order, rankType, (errCode, cmd, data)=>{
            // 返回错误码信息
            if (errCode !== 0) {
                STA.log('获取排行榜数据失败!错误码：' + errCode);
            }else if(data){
                STA.log('成功获取排行榜信息！');
                this.rankList = data.data.ranking_list;
                this.rankContent.height = this.aRankHeight * this.rankList.length;

                for(let i=0; i < this.rankList.length; i++) {
                    let li = this.rankList[i];
                    let aRankNode = cc.instantiate(this.aRank);
                    aRankNode.active = true;

                    getTheRankNumImg(aRankNode.getChildByName("rankNumber"), i+1);

                    aRankNode.getChildByName("nick").getComponent(cc.Label).string = li.nick;
                    cc.loader.load({url:li.url,type:'jpg'}, (err, texture)=>{
                        if(err) throw err;
                        let sprite = new cc.SpriteFrame(texture);
                        let userImg = aRankNode.getChildByName("userImgMask").getChildByName("userImg");
                        userImg.getComponent(cc.Sprite).spriteFrame = sprite;
                        userImg.width = 120;
                        userImg.height = 120;
                    });

                    aRankNode.getChildByName("chapterNumber").getComponent(cc.Label).string = "第"+li.score+"关";
                    
                    aRankNode.y -= this.aRankHeight * (i + 1);
                    this.rankContent.addChild(aRankNode);

                    if(li.selfFlag){  // 如果是自己，则再加载一次
                        let myRankNode = cc.instantiate(this.aRank);
                        myRankNode.active = true;

                        getTheRankNumImg(myRankNode.getChildByName("rankNumber"), i+1);

                        myRankNode.getChildByName("nick").getComponent(cc.Label).string = li.nick;
                        cc.loader.load({url:li.url,type:'jpg'}, (err, texture)=>{
                            if(err) throw err;
                            let sprite = new cc.SpriteFrame(texture);
                            let userImg = myRankNode.getChildByName("userImgMask").getChildByName("userImg");
                            userImg.getComponent(cc.Sprite).spriteFrame = sprite;
                            userImg.width = 120;
                            userImg.height = 120;
                        });

                        myRankNode.getChildByName("chapterNumber").getComponent(cc.Label).string = "第"+li.score+"关";
                        
                        myRankNode.y = -1/2 * this.aRankHeight;
                        this.myRank.addChild(myRankNode);
                    }
                }
            }
        });
    },

    closeRank(event){
        event.target.runAction(cc.sequence(
            cc.scaleTo(0.2,1.2),
            cc.scaleTo(0.2,1),
            cc.callFunc(()=>{
                this.rankBlank.active = false;
            })
        ));
        STA.playAudio(this.tapSound);
    },

    share(){
        STA.share("看谁能一气呵成最佳图案？",()=>{
            this.showSuccessTip(100);
        });
    },

    strategy(){
        console.log("stategy1")
        if(STA.isQQ){
            console.log("stategy2")
            BK.MQQ.Webview.open("https://jq.qq.com/?_wv=1027&k=5khqo6L");
        }
    },

    showSuccessTip(n){
        // 分享成功，获得 n 金币
        this.tipFrame.active = true;
        let tipText = this.tipFrame.getChildByName("tip_text");
        tipText.getComponent(cc.Label).string = `分享成功，获得${n}金币！`;
        let btn = this.tipFrame.getChildByName("orangeBtn");
        btn.active = true;
        btn.getChildByName("text").getComponent(cc.Label).string = "好";
        let _this = this;
        btn.on(cc.Node.EventType.TOUCH_START, (event)=>{
            btn.runAction(cc.sequence(
                cc.scaleTo(0.15,1.1),
                cc.scaleTo(0.15,1),
                cc.callFunc(()=>{
                    _this.tipFrame.active = false;
                    btn.active = false;
                })
            ));
            STA.playAudio(_this.tapSound);
        })
        STA.increaseCoin(n);
    },

    update (dt) {},
});
